package com.cloudptt.api.product.func.voice;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.ProductOperator;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.VoiceOperator;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class VoiceReport extends Thread
{
	private static final String APPID = "appid=1442820561";//0x55ffb1d1
	private Context ctx = null;
	private VoiceOperator operator = null;

	private BlockingQueue<String> voiceMsgList = null;
	private AlertDialog voiceReportDlg = null;
	private volatile boolean isUsed = false;
	private volatile boolean isStop = false;
	private String lanType = "";
	private ProductOperator p_operator;

	public VoiceReport(Context ctx,String lanType,ProductOperator operator)
	{
		this.ctx = ctx;
		this.lanType = lanType;
		this.voiceMsgList = new LinkedBlockingQueue<String>();//new PriorityBlockingQueue<String>();
		p_operator = operator;
		
	}

	public boolean isUsed() {
		return isUsed;
	}
	public void setOperator(VoiceOperator operator) {
		this.operator = operator;
	}

	public void addVoiceMsg(String msgStr)
	{
		Log.d(VoiceReportFunc.Log_Key, "addVoiceMsg msgStr = " + msgStr);
		//		msgStr = "娴嬭瘯璇煶:鐏溅鏉ュ暒";
		this.voiceMsgList.add(msgStr);
	}
	public void stopThread() 
	{
		this.isStop = true;
	}

	public void run()
	{
		Log.d(VoiceReportFunc.Log_Key, "VoiceReport run");
//		while(!isStop)
//		{
//			String msg;
//			try {
//				msg = voiceMsgList.take( );
//				reportVoice(msg);
////				if(!isUsed)
////				{
////					reportVoice(msg);
////				}
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}

	private void reportVoice(String voiceReportStr)
	{

		if(voiceReportStr != null
				&& voiceReportStr.length() > 0)
		{
			Log.d(VoiceReportFunc.Log_Key, "reportVoice voiceReportStr = " + voiceReportStr);

			isUsed = true;
//			if(operator != null)
//			{
//				operator.clearAllVoice();
//			}

//			if(Product.getTerminalType() != ProductConst.TerminalType_WP)
//			{
				Message msg = handler.obtainMessage(MsgId_ShowDlg);
				msg.obj = voiceReportStr;
				msg.sendToTarget();
//			}

			//			voiceReportStr = voiceReportStr + voiceReportStr+ voiceReportStr+ voiceReportStr+ voiceReportStr;
//			SynthesizerPlayer player = SynthesizerPlayer.createSynthesizerPlayer(ctx, APPID);
			//璁剧疆璇煶鏈楄鑰咃紝鍙互鏍规嵁闇�瑕佽缃敺濂虫湕璇伙紝鍏蜂綋璇风湅api鏂囨。鍜屽畼鏂硅鍧�
//			if(SysConfig.LAN_EN_US.equals(lanType))
//			{
//				player.setVoiceName("Catherine");//鍦ㄦ璁剧疆璇煶鎾姤鐨勪汉閫変緥濡傦細vivixiaoyan銆乿ivixiaomei銆乿ivixiaoqi
//			}
//			else
//			{
//				player.setVoiceName("vivixiaoyan");
//			}
//			player.playText(voiceReportStr, "ent=vivi21,bft=5",playListener);
		}
		else
		{
			Log.d(VoiceReportFunc.Log_Key, "reportVoice voiceReportStr = null");
		}
	}

//	private SynthesizerPlayerListener playListener = new SynthesizerPlayerListener()
//	{
//		@Override
//		public void onPlayBegin()
//		{
//		}
//
//		@Override
//		public void onBufferPercent(int arg0, int arg1, int arg2) 
//		{
//		}
//
//		@Override
//		public void onEnd(SpeechError arg0) 
//		{
//			Log.e(VoiceReportFunc.Log_Key, "onEnd");
////			if(Product.getTerminalType() != ProductConst.TerminalType_WP)
////			{
//				Message msg = handler.obtainMessage(MsgId_CloseDlg);
//				msg.sendToTarget();
////			}
//
//			if(operator != null)
//			{
////				if(GroupCallConst.SpeakState_Listen!= p_operator.getCallAgent().getState())
////				{
////					operator.renewAllVoice(false);
////				}
//				
//			}
//			isUsed = false;
//		}
//
//		@Override
//		public void onPlayPaused() 
//		{
//		}
//
//		@Override
//		public void onPlayPercent(int arg0, int arg1, int arg2) {
//		}
//
//		@Override
//		public void onPlayResumed() 
//		{
//		}
//	};

	private static int MsgId_ShowDlg = 1;
	private static int MsgId_CloseDlg = 2;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler()
	{ 
		public void handleMessage(Message msg)
		{ 
			super.handleMessage(msg); 
			Log.d(VoiceReportFunc.Log_Key, "handleMessage msg = " + msg);

			if(MsgId_ShowDlg == msg.what)
			{
				if(voiceReportDlg != null)
				{
					voiceReportDlg.dismiss();
					voiceReportDlg = null;
				}
				voiceReportDlg = CommDlg.showVoiceReportDlg(ctx,msg.obj.toString());
			}
			else if(MsgId_CloseDlg == msg.what)
			{
				if(voiceReportDlg != null)
				{
					voiceReportDlg.dismiss();
					voiceReportDlg = null;
				}
			}
		} 
	};
	
	public void setLanType(String lanType) {
		this.lanType = lanType;
	}
}
